<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<form>
    <input type="hidden" name="id" id="id">
    用户名：<input type="text" name="username" id="username"><br/>
    用户密码：<input type="password" name="password" id="password"><br/>
    用户角色：<select id="role" ></select><br/>
    <input type="button" value="修 改" id="submit">
</form>
</body>
<script>
    $(function (){
        $("#role").empty();
        $.ajax({
            type:"post",
            url:"/user/getAllRole",
            success:function (res){
                for (const re of res) {
                    $("#role").append($("<option></option>").val(re.roleid).text(re.rolename))
                }
                getuserInfo();
            }
        })
    })
    function getuserInfo (){
        $.ajax({
            type:"post",
            url:"/user/getUserInfo",
            success:function (res){
                $("#id").val(res.id)
                $("#username").val(res.username)
                $("#password").val(res.password)
                $("#role").val(res.role.roleid)
            }
        })
    }
    $("#submit").click(function (){
        $.ajax({
            type:"post",
            url:"/user/updateUser",
            contentType: "application/json", // 设置内容类型为 JSON
            data: JSON.stringify({
                id: $("#id").val(),
                username: $("#username").val(),
                password: $("#password").val(),
                role:{
                    roleid:$("#role").val()
                }
            }),
            success: function (res){
                if (res=="true"){
                    location.href="userInfo.html"
                } else {
                    alert("修改失败")
                }
            }
        })
    })
</script>

</html>
